package com.shangma.cn.controller;

import com.shangma.cn.commons.http.AxiosResult;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.commons.valid.AddGroup;
import com.shangma.cn.commons.valid.UpdateGroup;
import com.shangma.cn.controller.base.BaseController;
import com.shangma.cn.dto.base.impl.AdminDTO;
import com.shangma.cn.entity.base.impl.Admin;
import com.shangma.cn.perm.HasPerm;
import com.shangma.cn.query.impl.AdminQuery;
import com.shangma.cn.service.AdminService;
import com.shangma.cn.service.AdminService;
import com.shangma.cn.transfer.base.impl.AdminTransfer;
import com.shangma.cn.transfer.base.impl.AdminTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/7  16:42
 * 文件含义:
 */
@RestController
@RequestMapping("admin")
@RequiredArgsConstructor
public class AdminController extends BaseController {

    /*
     * 前后端分离的时候
     *  前端和后台 可能不是一个人写的
     *
     * 当前端访问后台的接口时
     *       1： 怎么知道请求路径的
     *       2： 怎么知道需要什么参数
     *       3： 怎么知道返回值的
     *
     * 此时我们前后端程序员需要一个接口文档
     *    1: 在线的
     *         swagger2 knife4j
     *
     *
     *    2: 纸质版的 （电子版的）
     *
     *
     *    3： postman
     *
     * */

    public final AdminService adminService;

    public final AdminTransfer adminTransfer;

    public final BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 分页条件查询
     *
     * @return
     */
    @GetMapping
    @HasPerm(permSign = "sys:admin:list")
    public AxiosResult<PageBean<AdminDTO>> searchPage(AdminQuery AdminQuery) {
        return AxiosResult.success(adminService.searchPage(AdminQuery));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AxiosResult<AdminDTO> getById(@PathVariable long id) {
        return AxiosResult.success(adminService.getAdminAndRolesById(id));
    }


    @PostMapping
    @HasPerm(permSign = "sys:admin:add")
    public AxiosResult<Void> add(@RequestBody @Validated(AddGroup.class) Admin admin) {
        return toAjax(adminService.addAdminAndRoles(admin));
    }

    @PutMapping
    @HasPerm(permSign = "sys:admin:update")
    public AxiosResult<Void> update(@RequestBody @Validated(UpdateGroup.class) Admin Admin) {
        return toAjax(adminService.updateAdminAndRoles(Admin));

    }

    @DeleteMapping("{id}")
    public AxiosResult<Void> deleteById(@PathVariable long id) {
        return toAjax(adminService.deleteById(id));
    }

    @DeleteMapping("batch/{ids}")
    @HasPerm(permSign = "sys:admin:delete")
    public AxiosResult<Void> batchDelete(@PathVariable List<Long> ids) {
        return toAjax(adminService.deleteAdminCascadeRoles(ids));
    }
}
